<template>
	<view class="content">
		<!-- #ifdef APP-PLUS || H5 -->
		<view class="content echarts" @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts"
			:id="this.id"></view>
		<!-- #endif -->
		<!-- #ifndef APP-PLUS || H5 -->
		<view>非 APP、H5 环境不支持</view>
		<!-- #endif -->
	</view>
</template>

<script>
	import echarts from "../../static/common/js/echarts.min.js"
	export default {
		name: 'chartPie',
		props: {
			chartPieData:{
				type: Object,
				default() {
					return {}
				}
			}
		},
		data() {
			return {
				id:'',
				color: ['#BADEFF', '#C1EDCE', '#FEF0B9'],
				option: {
					//图表title
					// title: {
					// 	text: '饼图'
					// },
					//饼图各块的颜色
					color: ['#4FA0FF', '#5FCB6D', '#EA6479', '#F6D203', '#9561E9', '#CB5FB9', '#695FCB', '#CB8A5F',
						'#0672BF'
					],
					tooltip: {
						trigger: 'axis'
					},
					//图例
					legend: {
						//图例数据
						data: [],
						//是否显示图例
						show: false,
						x: 'center',
						bottom: '5%',
						itemWidth: 10,
						itemHeight: 8,
						textStyle: { //图例文字的样式
							color: '#999',
							fontSize: 10
						},
					},
					//整个图表区域
					grid: {
						top: '1%',
						left: '3%',
						right: '4%',
						bottom: '0',
						containLabel: true
					},
					//图表数据
					series: [{
						name: '饼图',
						type: 'pie',
						radius: '50%',
						//标示线
						labelLine: {
							normal: {
								length: 8, // 改变标示线的长度
								lineStyle: {
									color: "#999" // 改变标示线的颜色
								}
							},
						},
						//各块文字说明
						label: {
							color: '#999',
							minMargin: 8,
							edgeDistance: 10,
							lineHeight: 15,
						},
						//饼图各组数据
						data: [],
						emphasis: {
							itemStyle: {
								shadowBlur: 10,
								shadowOffsetX: 0,
								shadowColor: 'rgba(0, 0, 0, 0.5)'
							}
						}
					}]
				}
			}
		},
		mounted() {
			this.handleChartPieData()
		},
		watch: {
			chartPieData: function(newQuestion, oldQuestion) {
				this.handleChartPieData()
			}
		},
		methods: {
			handleChartPieData() {
				// if(this.chartPieData.legendData) {
				// 	this.option.legend.data = this.chartPieData.legendData;
				// }
				// if(this.chartPieData.pieData) {
				// 	this.option.series[0].data = this.chartPieData.pieData;
				// }
				console.log(this.chartPieData)
				this.option=this.chartPieData.option;
				this.id=this.chartPieData.id;
			},
			
			onViewClick(options) {
				// console.log(options)
			}
		}
	}
</script>

<script module="echarts" lang="renderjs">
	let myChart
	export default {
		mounted() {
			if (typeof window.echarts === 'function') {
				this.initEcharts()
			} else {
				// 动态引入较大类库避免影响页面展示
				const script = document.createElement('script')
				// view 层的页面运行在 www 根目录，其相对路径相对于 www 计算
				script.src = 'static/common/js/echarts.min.js'
				script.onload = this.initEcharts.bind(this)
				document.head.appendChild(script)
			}
		},
		methods: {
			initEcharts() {
				// myChart = echarts.init(document.getElementById('echartspie'))
				let domid=this.chartPieData.id || 'echartspie';
				myChart = echarts.init(document.getElementById(domid))
				this.option.series[0].label.formatter = function(params) {
					return params.data.descripetion;
				}

				myChart.setOption(this.option)
			},
			updateEcharts(newValue, oldValue, ownerInstance, instance) {
				// 监听 service 层数据变更
				if (myChart !== undefined) {
					myChart.setOption(newValue)
				}
			},
			onClick(event, ownerInstance) {
				// 调用 service 层的方法
				// ownerInstance.callMethod('onViewClick', {
				// 	test: 'test'
				// })
			}
		}
	}
</script>

<style scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.echarts {
		width: 100%;
		height: 400rpx;
	}
</style>
